﻿Public Class xtf_LISTADOdocumentoalteracion_

    Public subTotal As Decimal

    Public Sub New(ByRef prm_datatable As DataTable, ByRef prm_col_eliminado_ As Collections.Generic.List(Of cls_documentoalteracion_), ByVal prm_subTotal As Decimal)
        InitializeComponent()
        Me.Text = "Alteracion de Documento"
        btn_Guardar.Visible = False
        cbo_tipo.SelectedIndex = 0

        subTotal = prm_subTotal

        gdv_documentoalteracion_.Tag = prm_col_eliminado_
        gdc_documentoalteracion_.DataSource = prm_datatable
        gdc_documentoalteracion_.ForceInitialize()
        gdv_documentoalteracion_.PopulateColumns()
        gdv_documentoalteracion_.Columns("Objeto").Visible = False

        gdv_documentoalteracion_.Columns("Nombre").Width = NoVal_Nombre.Width + 3
        gdv_documentoalteracion_.Columns("Tipo").Width = NoVal_Tipo.Width + 3
        gdv_documentoalteracion_.Columns("Formula").Width = NoVal_Formula.Width + 3
        gdv_documentoalteracion_.Columns("Total").Width = NoVal_Total.Width + 3

        'For Each documentoalteracion As cls_documentoalteracion_ In col_documentoalteracion_
        '    Dim row As DataRow = dtt_documentoalteracion_.NewRow
        '    row("Nombre") = documentoalteracion.nombre
        '    row("Tipo") = documentoalteracion.tipo
        '    row("Valor") = documentoalteracion.formula
        '    row("Objeto") = documentoalteracion
        '    dtt_documentoalteracion_.Rows.Add(row)
        'Next

    End Sub

    Private Sub tsb_documentoagrupacionAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb_documentoagrupacionAgregar.Click
        Dim documentoalteracion As New cls_documentoalteracion_
        documentoalteracion.nombre = txt_nombre.Text
        documentoalteracion.tipo = cbo_tipo.SelectedIndex
        documentoalteracion.formula = txt_formula.Text

        Dim row As DataRow = gdc_documentoalteracion_.DataSource.NewRow
        row("Nombre") = documentoalteracion.nombre
        row("Tipo") = documentoalteracion.tipo
        row("Formula") = documentoalteracion.formula

        Select Case cbo_tipo.SelectedIndex
            Case cls_alteracion_.enu_tipo.Valor
                row("Total") = txt_formula.Text
            Case cls_alteracion_.enu_tipo.Porcentaje
                row("Total") = Math.Round((txt_formula.Text * subTotal) / 100, int_cantdecimales)
            Case cls_alteracion_.enu_tipo.Formula
                row("Total") = Math.Round(documentoalteracion.Resolver(subTotal), int_cantdecimales)
        End Select

        If Not IsNothing(txt_nombre.Tag) Then
            documentoalteracion.idalteracion = CType(txt_nombre.Tag, cls_alteracion_).idalteracion
        End If
        row("Objeto") = documentoalteracion

        gdc_documentoalteracion_.DataSource.Rows.Add(row)

        txt_formula.Text = 0
        txt_nombre.Text = ""
        txt_Valor.Text = 0
        txt_nombre.Focus()

    End Sub

    Private Sub tsb_documentoagrupacionEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb_documentoagrupacionEliminar.Click
        If gdv_documentoalteracion_.SelectedRowsCount > 0 Then
            gdv_documentoalteracion_.BeginDataUpdate()
            gdv_documentoalteracion_.Tag.Add(gdv_documentoalteracion_.GetDataRow(gdv_documentoalteracion_.GetSelectedRows(0))("Objeto"))
            gdc_documentoalteracion_.DataSource.Rows.Remove(gdv_documentoalteracion_.GetDataRow(gdv_documentoalteracion_.GetSelectedRows(0)))
            gdv_documentoalteracion_.EndDataUpdate()
        End If
    End Sub

    Private Sub cbo_tipo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cbo_tipo.KeyPress
        If e.KeyChar = Chr(13) Then
            txt_formula.Focus()
        End If
    End Sub

    Private Sub cbo_tipo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo_tipo.SelectedIndexChanged
        If cbo_tipo.SelectedIndex = 2 Then
            txt_formula.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.None
            txt_formula.Properties.Mask.EditMask = ""
            txt_formula.Properties.Buttons(0).Visible = True
            txt_formula.Properties.ReadOnly = True
        Else
            txt_formula.Properties.Buttons(0).Visible = False
            txt_formula.Properties.ReadOnly = False
            txt_formula.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric
            txt_formula.Properties.Mask.EditMask = "n" & int_cantdecimales
        End If
        txt_formula_EditValueChanged(Nothing, Nothing)
    End Sub

    Private Sub txt_formula_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles txt_formula.ButtonClick
        Dim ABMFormula As New xtf_ABMFormula(txt_formula.Text)
        If ABMFormula.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txt_formula.Text = ABMFormula.txt_formula.Text
        End If
    End Sub

    Private Sub txt_formula_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_formula.EditValueChanged
        Select Case cbo_tipo.SelectedIndex
            Case cls_alteracion_.enu_tipo.Valor
                txt_Valor.Text = txt_formula.Text
            Case cls_alteracion_.enu_tipo.Porcentaje
                txt_Valor.Text = Math.Round((txt_formula.Text * subTotal) / 100, int_cantdecimales)
        End Select
    End Sub

    Private Sub txt_formula_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_formula.KeyPress
        If e.KeyChar = Chr(13) Then
            tsb_documentoagrupacionAgregar.Focus()
        End If
    End Sub

    Private Sub txt_nombre_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles txt_nombre.ButtonClick
        Dim buscar As xtf_buscardor = Nothing
        Dim Objeto_ As New cls_alteracion_
        Objeto_.CargarTodos()

        buscar = New xtf_buscardor("Alteraciones", Objeto_, {"nombre|Nombre", "descripcion|Descripcion"})

        If buscar.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim alteracion As cls_alteracion_ = buscar.obj_resultado
            txt_nombre.Tag = alteracion
            txt_nombre.Text = alteracion.nombre
            cbo_tipo.SelectedIndex = alteracion.tipo
            txt_formula.Text = alteracion.formula

            txt_nombre.Properties.ReadOnly = True
        End If
    End Sub

    Private Sub txt_nombre_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_nombre.KeyDown
        If e.KeyCode = Keys.F3 Then
            txt_nombre.PerformClick(txt_nombre.Properties.Buttons(0))
            Exit Sub
        End If
        If e.KeyCode <> 13 Then
            If Not IsNothing(txt_nombre.Tag) Then
                txt_nombre.Tag = Nothing
            End If
            txt_nombre.Properties.ReadOnly = False
        End If
        
    End Sub

    Private Sub txt_nombre_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_nombre.KeyPress
        If e.KeyChar = Chr(13) Then
            cbo_tipo.Focus()
        Else
            If Not IsNothing(txt_nombre.Tag) Then
                txt_nombre.Tag = Nothing
            End If
            txt_nombre.Properties.ReadOnly = False
        End If
    End Sub

End Class